package com.dgshanger.sanhxiaofeisc.mvp.presenter;

import com.dgshanger.sanhxiaofeisc.entity.InvoiceEntity;
import com.dgshanger.sanhxiaofeisc.mvp.contract.InvoiceAssistantContract;
import com.dgshanger.sanhxiaofeisc.mvp.model.InvoiceAssistantModel;
import com.xx.baseuilibrary.mvp.BaseRxMvpPresenter;
import com.xx.baseutilslibrary.network.rx.RxHttpObserver;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * InvoiceAssistantPresenter
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/5/3 18:41.
 */

public class InvoiceAssistantPresenter extends BaseRxMvpPresenter<InvoiceAssistantContract.Model, InvoiceAssistantContract.View> implements InvoiceAssistantContract.Presenter {

    @NotNull
    @Override
    protected InvoiceAssistantContract.Model createModel() {
        return new InvoiceAssistantModel();
    }

    @Override
    public void myInvStore() {
        if (getView() == null) {
            return;
        }

        String invoiceHeader = getView().getInvoiceHeader();
        String invoiceAddress = getView().getInvoiceAddress();
        String invoiceBankName = getView().getInvoiceBankName();
        String invoiceBaseAccount = getView().getInvoiceBaseAccount();
        String invoiceCompanyName = getView().getInvoiceCompanyName();
        String invoiceMobile = getView().getInvoiceMobile();
        String invoiceRegisterNumber = getView().getInvoiceRegisterNumber();
        String invoiceTaxNumber = getView().getInvoiceTaxNumber();

        getModel()
                .myInvStore(invoiceHeader, invoiceCompanyName, invoiceBaseAccount, invoiceBankName, invoiceTaxNumber, invoiceRegisterNumber,
                        invoiceMobile, invoiceAddress, new RxHttpObserver<Object>() {
                            @Override
                            public void onCompleted(@Nullable String msg, @Nullable Object entity) {
                                if (getView() == null) {
                                    return;
                                }
                                getView().showToast(msg);
                                getView().finishActivity();
                            }

                            @Override
                            public void onError(@Nullable String error) {
                                if (getView() == null) {
                                    return;
                                }
                                getView().showToast(error);
                            }

                            @Override
                            public void onLoadingStart() {
                                attachObserver(this);
                                if (getView() == null) {
                                    return;
                                }
                                getView().showLoadingDialog();
                            }

                            @Override
                            public void onLoadingFinish() {
                                if (getView() == null) {
                                    return;
                                }
                                getView().dismissLoadingDialog();
                            }
                        });
    }

    @Override
    public void myInv() {
        getModel().myInv(new RxHttpObserver<InvoiceEntity>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable InvoiceEntity entity) {
                if (getView() == null) {
                    return;
                }
                getView().setInvoiceEntity(entity);
            }

            @Override
            public void onError(@Nullable String error) {

            }

            @Override
            public void onLoadingStart() {
                attachObserver(this);
            }

            @Override
            public void onLoadingFinish() {

            }
        });
    }
}
